package com.ykg.service.impl;

import com.ykg.dto.ContractDTO;
import com.ykg.mapper.ContractMapper;
import com.ykg.mapper.EntrustMapper;
import com.ykg.pojo.Contract;
import com.ykg.pojo.Entrust;
import com.ykg.pojo.NoticeLetter;
import com.ykg.request.CheckContractRequest;
import com.ykg.service.IContractService;
import com.ykg.service.IDTOService;
import com.ykg.util.CreateIdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 类(接口)：
 * Created on 2021/6/23 15:14.
 * Author: zetong
 * Description:
 */
@Service
public class ContractServiceImpl implements IContractService {

    @Autowired
    private ContractMapper contractMapper;
    @Autowired
    private IDTOService idtoService;
    @Autowired
    private EntrustMapper entrustMapper;


    @Override//查看合同列表
    public List<Contract> getContract(){
        List<Contract> contracts = contractMapper.findAll();
        return contracts;
    }
    @Override//查看合同信息
    public ContractDTO contractDetailed(String contractId){
        Contract contract = contractMapper.findById(contractId);
        ContractDTO contractDTO =  idtoService.createContractDto(contract);
        return contractDTO;
    }
    @Override//办理合同
    public int checkContract(CheckContractRequest checkContractRequest){
        int result1 = contractMapper.updateContract(checkContractRequest.getContractId(),checkContractRequest.getBeginDate(),checkContractRequest.getEndDate(),
                checkContractRequest.getEarnestmoney(),checkContractRequest.getSurrendermoney(),checkContractRequest.getHandlingcharges());

        CreateIdUtil createIdUtil= new CreateIdUtil();
        String entrustId = createIdUtil.creatEntrustId();
        Contract contract = contractMapper.findById(checkContractRequest.getContractId());
        //生成待处理的委托单
        int state = 0;//未处理
        int endState = 0;//未开始清仓
        Entrust entrust = new Entrust(entrustId,contract.getOwnerId(),contract.getShipId(),contract.getListId(),state,endState);
        int result2 = entrustMapper.insertEntrust(entrust);

        return result1+result2;
    }
}
